<?php
use \Workerman\Autoloader;

require_once __DIR__ . '/../../../../../Workerman/Autoloader.php';
require_once __DIR__.'/../../../Config/Const.php';
Autoloader::setRootPath(__DIR__.'/../../..');

$page_groupId = GROUP_TREE_MNG;


require "../framepages/moudle_base.php";


$species_id = $_GET['species_id'];
$group_id = $_GET['group_id'];
$species_record = \Data\Tree_species_DAO::getRecord($species_id);
$species_name = $species_record->getSPECIES_NAME();
$gg_id = $species_record->getGG_ID();
$gg_record = \Data\Tree_gg_DAO::getRecord($gg_id);
$gg_name = $gg_record->getGG_NAME();
$gg_dw = $gg_record->getGG_DW();
$gg = $gg_name."(".$gg_dw.")";
$group_record = \Data\Tree_group_DAO::getRecord($group_id);
$gg_string = $group_record->getGG_ARRAY();
if(!$gg_string) {
    $gg_array=array();
} else {
    $gg_array = explode(',' ,$gg_string);
}

$mode = $_POST['mode'];
$result = 'normal';
if ($mode == 'new_gg_cs_add') {
    $new_gg_cs = $_POST['new_gg_cs'];
    if (in_array($new_gg_cs,$gg_array)) {
        $result = 'already_exist';
    } else {
        $gg_array[] = $new_gg_cs;
        sort($gg_array);
        $gg_string = implode(',',$gg_array);
        $group_record->setGG_ARRAY($gg_string);
        \Data\Tree_group_DAO::update($group_record);
        $result = 'succ_add_new_gg_cs';
    }
}

$mode_act = $_GET['mode_act'];
if ($mode_act == 'delete_group_gg') {
    $gg_cs = $_GET['gg_cs'];
    // 该参数产品列表
    $productor_list = \Data\Tree_productor_DAO::getListByGroup($group_id,$gg_cs);
    if ($productor_list) {
        $result = 'err_exist_productor';
    } else {
        $gg_array = explode(',' ,$gg_string);
        foreach ($gg_array as $key=>$value)
        {
            if ($value == $gg_cs) {
                unset($gg_array[$key]);
            }
        }
        $gg_string = implode(',',$gg_array);
        $group_record->setGG_ARRAY($gg_string);
        \Data\Tree_group_DAO::update($group_record);
        $result = 'succ_delete';
    }
}


$group_record = \Data\Tree_group_DAO::getRecord($group_id);
$group_name = $group_record->getGROUP_NAME();
$gg_string = $group_record->getGG_ARRAY();
if(!$gg_string) {
    $gg_array=array();
} else {
    $gg_array = explode(',' ,$gg_string);
}
?>
<div class="main-content">
    <div style="margin-bottom: 25px">
        <table>
            <tr>
                <th width="12%"><span>花木名称：</span></th>
                <td width="12%"><span><?php echo $species_name ?></span></td>
                <th width="12%"><span>分组名称：</span></span></th>
                <td><span><?php echo $group_name ?></span></span></td>
            </tr>
        </table>

    </div>
    <div>
        <table class="table" style="word-break:break-all">
            <tr>
                <th width="9%">序号</th>
                <th width="9%">规格类型</th>
                <th width="9%">规格参数</th>
                <th width="12%">产品列表</th>
                <th>删除</th>
            </tr>

            <?php
            $i = 1;
            if ($gg_string) {
                foreach ($gg_array as $gg_cs) {
                    echo "
                        <tr>
                            <td>$i</td>
                            <td>$gg</td>
                            <td>$gg_cs</td>
                            <td><button type='button' class='btn btn-danger' onclick='productor_list($species_id,$group_id,$gg_cs)' >产品列表</button></td>                            
                            <td><button type='button' class='btn btn-danger' onclick='delete_group_gg($gg_cs)' >删除</button></td>                            
                        </tr>
                    ";
                    $i++;
                }
            }

            ?>

        </table>
    </div>

    <?php
    require "../framepages/footer.php";
    ?>
    <div>
        <div>
            <span><h2>添加规格参数</h2></span>
        </div>
        <form action="" method="post" onSubmit="javascript:return window.confirm('确认提交吗？')">
            <input type="hidden" name="mode" id="mode" value='new_gg_cs_add'>
            <table>
                <tr>
                    <td>规格参数</td>
                    <td><input type="text" name="new_gg_cs" placeholder="例如：3"></td>
                </tr>
            </table>

            <div style="margin-top:30px;">
                <input type="submit" class="btn btn-danger" value="提交">
            </div>
        </form>
    </div>
</div>
</div>
<script src="../lib/bootstrap/js/bootstrap.js"></script>
<script>

    function init() {
        var msg = "<?php echo $result; ?>";
        if (msg == 'already_exist') {
            alert('添加失败，已存在同规格');
        }
        if (msg == 'succ_add_new_gg_cs') {
            alert('添加成功');
        }
        if (msg == 'err_exist_productor') {
            alert('不能删除，该规格参数下还有产品');
        }
        if (msg == 'succ_delete') {
            alert('删除成功');
        }

        var mode = "<?php echo $mode ?>";
        var mode_act = "<?php echo $mode_act ?>";
        if (mode || mode_act) {
            window.location.href = "group_gg_list.php?species_id="+"<?php echo $species_id ?>"+"&group_id="+"<?php echo $group_id ?>";
        }

    }

    function productor_list(species_id,group_id,gg_cs) {
        var url = "productor_list.php?species_id="+species_id+"&group_id="+group_id+"&gg_cs="+gg_cs;
        window.location.href = url;
    }

    function delete_group_gg(gg_cs) {
        var url = "group_gg_list.php?species_id="+"<?php echo $species_id ?>"+"&group_id="+"<?php echo $group_id ?>";
        url = url+"&mode_act=delete_group_gg&gg_cs="+gg_cs;
        window.location.href = url;
    }
</script>
</body>
</html>
